# -*- coding: utf-8 -*-


def version_greater(ver1: [str, int, tuple, list], ver2: [str, int, tuple, list]):
    # 拼接字符串比数组慢
    type1 = type(ver1)
    type2 = type(ver2)
    # 都是int或str
    if type1 in (int, str):
        if type1 == type2:
            return ver1 > ver2
        elif type2 in (int, str):
            return str(ver1) > str(ver2)
    # 全部转tuple或list
    if type1 == str:
        ver1 = ver1.split('.')
    elif type1 == int:
        ver1 = (str(ver1),)
    if type2 == str:
        ver2 = ver2.split('.')
    elif type2 == int:
        ver2 = (ver2,)
    # 数组的元素类型不定，不能直接比较
    for i in range(min(len(ver1), len(ver2))):
        a = ver1[i]
        b = ver2[i]
        if not type(a) == type(b):
            a = str(a)
            b = str(b)
        if a == b:
            continue
        return a > b
    return len(ver1) > len(ver2)


def version_equals(ver1, ver2):
    type1 = type(ver1)
    type2 = type(ver2)
    # 都是int或str
    if type1 in (int, str):
        if type1 == type2:
            return ver1 == ver2
        elif type2 in (int, str):
            return str(ver1) == str(ver2)
    # 全部转tuple或list
    if type1 == str:
        ver1 = ver1.split('.')
    elif type1 == int:
        ver1 = (str(ver1),)
    if type2 == str:
        ver2 = ver2.split('.')
    elif type2 == int:
        ver2 = (ver2,)
    if len(ver1) == len(ver2):
        for i in range(len(ver1)):
            a = ver1[i]
            b = ver2[i]
            if not type(a) == type(b):
                a = str(a)
                b = str(b)
            if not a == b:
                return False
        return True
    else:
        return False


def version_less(ver1, ver2):
    return version_greater(ver2, ver1)


def version_not_greater(ver1, ver2):
    return not version_greater(ver1, ver2)


def version_not_less(ver1, ver2):
    return not version_greater(ver2, ver1)


def version_not_equals(ver1, ver2):
    return not version_equals(ver1, ver2)


def regularize_filename(filename: str, replace='_'):
    import re
    return re.sub(r'[\\/:*?"<>|]', replace, filename, flags=re.UNICODE)


if __name__ == "__main__":
    import time

    time.clock()
    for i in range(1000000):
        g = version_greater("4.4", "4.3")
        # g = version_greater((1,"3","2"), ("2","3","3"))
    print(time.clock())
    print(g)

    print(regularize_filename("192.168.43.59:5555"))
